using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NPlot;
using System.Data.SQLite;
using YahooStocks;

namespace Harness
{
    public partial class FormChart : Form
    {
        public FormChart()
        {
            InitializeComponent();
        }

        public void DoChart( string symbol, DateTime startDate, DateTime endDate)
        {
            string issueSQL = Constants.GetIssueDataSQLTemplate;
     issueSQL = issueSQL.Replace("[SYMBOL]", symbol);
            object[] parms = {startDate, endDate};
            

            DataSet ds =
                System.Data.SQLite.SQLiteHelper.ExecuteDataSet(Constants.ConnectionString, issueSQL,
                                                               parms);
          
           
            
            DataTable dt = ds.Tables[0];
            DataView dv = new DataView(dt);

            // create CandlePlot.
            CandlePlot cp = new CandlePlot();
            
            cp.DataSource = dt;
            cp.AbscissaData = "Date";
            cp.OpenData = "Open";
            cp.LowData = "Low";
            cp.HighData = "High";
            cp.CloseData = "Close";
            cp.BearishColor = Color.Red;
            cp.BullishColor = Color.Green;
            cp.Style = CandlePlot.Styles.Filled;
            costPS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            costPS.Add(new Grid());
            costPS.Add(cp);
            costPS.Title = symbol;
            costPS.YAxis1.Label = "Price [$]";
            costPS.YAxis1.LabelOffset = 40;
            costPS.YAxis1.LabelOffsetAbsolute = true;
            costPS.XAxis1.HideTickText = true;
            costPS.Padding = 5;
            costPS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
            costPS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
            costPS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(false));
            costPS.InteractionOccured += new NPlot.Windows.PlotSurface2D.InteractionHandler(costPS_InteractionOccured);
            costPS.AddAxesConstraint(new AxesConstraint.AxisPosition(PlotSurface2D.YAxisPosition.Left, 60));

            PointPlot pp = new PointPlot();
            pp.Marker = new Marker(Marker.MarkerType.Square, 0);
            pp.Marker.Pen = new Pen(Color.Red, 5.0f);
            pp.Marker.DropLine = true;
            pp.DataSource = dt;
            pp.AbscissaData = "Date";
            pp.OrdinateData = "Volume";
            volumePS.Add(pp);
            volumePS.YAxis1.Label = "Volume";
            volumePS.YAxis1.LabelOffsetAbsolute = true;
            volumePS.YAxis1.LabelOffset = 40;
            volumePS.Padding = 5;
            volumePS.AddAxesConstraint(new AxesConstraint.AxisPosition(PlotSurface2D.YAxisPosition.Left, 60));
            volumePS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(false));
            volumePS.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
            volumePS.InteractionOccured += new NPlot.Windows.PlotSurface2D.InteractionHandler(volumePS_InteractionOccured);
            volumePS.PreRefresh += new NPlot.Windows.PlotSurface2D.PreRefreshHandler(volumePS_PreRefresh);

           // this.costPS.RightMenu = new ReducedContextMenu();
			
        }

        protected override void OnResize(EventArgs e)
        {
            this.costPS.Height = (int)(0.5 * (this.Height));
            this.costPS.Width = (int)(this.Width - 35);

            this.volumePS.Top = costPS.Height + 12;
            this.volumePS.Height = this.Height - (costPS.Height + 25) - 80;
            this.volumePS.Width = (int)(this.Width - 35);

            base.OnResize(e);
      }

      /// <summary>
      /// When the costPS chart has changed, this is called which updates the volumePS chart.
      /// </summary>
      /// <param name="sender"></param>
      private void costPS_InteractionOccured(object sender)
      {
          DateTimeAxis axis = new DateTimeAxis(costPS.XAxis1);
          axis.Label = "Date / Time";
          axis.HideTickText = false;
          this.volumePS.XAxis1 = axis;
          this.volumePS.Refresh();
      }


      /// <summary>
      /// When the volumePS chart has changed, this is called which updates hte costPS chart.
      /// </summary>
      private void volumePS_InteractionOccured(object sender)
      {
          DateTimeAxis axis = new DateTimeAxis(volumePS.XAxis1);
          axis.Label = "";
          axis.HideTickText = true;
          this.costPS.XAxis1 = axis;
          this.costPS.Refresh();
      }

      /// <summary>
      /// This is called prior to volumePS refresh to enforce the WorldMin is 0. 
      /// This may have been changed by the axisdrag interaction.
      /// </summary>
      /// <param name="sender"></param>
      private void volumePS_PreRefresh(object sender)
      {
          volumePS.YAxis1.WorldMin = 0.0;
      }

        private void FormChart_Load(object sender, EventArgs e)
        {

        }

        private void costPS_Click(object sender, EventArgs e)
        {

        }

       
    }
}